package com.im.nero.user;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import com.im.nero.user.entity.Student;
import com.im.nero.user.entity.Subject;
import com.im.nero.user.entity.Teacher;
import com.im.nero.user.entity.User;
import com.im.nero.user.entity.UserRole;

public class LoginAction {
	private String role;

	private String username;
	private String password;

	public String execute() {

		System.out.println(role);
		System.out.println(username);
		System.out.println(password);
		
		User user = getUser(username, password);

		if (user!=null) {
			return user.getUserRole().getCode();
		} else {
			return "error";
		}
	}
	
    public static User getUser(String username, String password) {
    	Configuration config = new Configuration();
		SessionFactory sf = config.configure().buildSessionFactory();
		Session session = sf.openSession();
        List<User> userList = new ArrayList<User>();
        Query query = session.createQuery("from User u where u.username = :username and u.password = :password");
        query.setParameter("username", username);
        query.setParameter("password", password);
        userList = query.list();
        if (userList.size() > 0)
            return userList.get(0);
        else
            return null;   
    }

	public static void main(String[] args) {
		Configuration config = new Configuration();
		SessionFactory sf = config.configure().buildSessionFactory();
		Session session = sf.openSession();
		Transaction transaction = session.beginTransaction();
		try {
			/*
			 * session.save(adrs1); session.save(adrs2); session.save(member);
			 */
			// session.getTransaction().commit();
			// PrintWriter out = response.getWriter();
			// out.print("<h1>Thank you foe Registeringwith us</h1>");
			// out.println("<a href=\"index.jsp\">Home</a>");
			User usr = new User();
			usr.setName("Test User1");
			UserRole userRole = new UserRole();
			userRole.setCode("Student");
			userRole.setDescription("Student Role");
			userRole.setPermission("Student");
			// usr.setUserRole(userRole );
			usr.setEmail("testUser1@gmail.com");
			usr.setContactNo("0718359030");
			usr.setCreatedTime(new Date());
			Student student = new Student();
			student.setId(usr.getId());
			student.setSchool("School of test");
			student.setDistrict("Colombo");
			// usr.setStudent(student);
			Set<Subject> subjects = new HashSet<Subject>();
			Subject sub = new Subject();
			sub.setId(1);
			subjects.add(sub);
			// usr.setSubjects(subjects);
			Subject sb = new Subject();
			sb.setName("Biology");
			sb.setDescription("A/L Combined Biology");
			session.save(sb);
			session.getTransaction().commit();
			Subject re = (Subject) session.get(Subject.class, 2);
			if (re != null) {
				System.out.println("got: " + re.getId());
			} else {
				System.out.println("Null");
			}
		} catch (Exception e) {
			e.printStackTrace();
			transaction.rollback();
		}
	}

	public String getRole() {
		return role;
	}

	public void setRole(String role) {
		this.role = role;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
}